package com.dxjy.demo.module.sys.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Author: zhang
 * @Date: 2021/8/31 11:57
 * @Description:
 * @Version: 1.0
 */
@Repository
public class StringRedis {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private SetOperations<String, String> getSetOperation() {
        return stringRedisTemplate.opsForSet();
    }

    private ValueOperations<String, String> getValueOperation() {
        return stringRedisTemplate.opsForValue();
    }

    public void set(String key, String value) {
        getSetOperation().add(key, value);
    }

    public Set<String> get(String key) {
        return getSetOperation().members(key);
    }

    public void setValue(String key, String value, long time) {
        if (time == 0) {
            getValueOperation().set(key, value);
        } else {
            getValueOperation().set(key, value, time);
        }
    }

    public String getValue(String key) {
        return getValueOperation().get(key);
    }

    public void setValue(RedisKeyEnum redisKeyEnum, String key, String value) {
        getValueOperation().set(redisKeyEnum.getKey(key), value, redisKeyEnum.getTime(), TimeUnit.MILLISECONDS);
    }

    public String getValue(RedisKeyEnum redisKeyEnum, String key) {
        return getValueOperation().get(redisKeyEnum.getKey(key)); // 取出
    }



}
